package com.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.gulimall.product.entity.CategoryBrandRelationEntity;
import com.gulimall.product.service.impl.CategoryBrandRelationServiceImpl;
import com.gulimall.product.service.impl.CategoryServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gulimall.product.entity.CategoryEntity;
import com.common.utils.PageUtils;
import com.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品三级分类
 *
 * @author Rookie-886
 * @email ${email}
 * @date 2021-03-14 16:30:53
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Resource
    private CategoryServiceImpl categoryServiceImpl;

    @Resource
    private CategoryBrandRelationServiceImpl categoryBrandRelationServiceImpl;
    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryServiceImpl.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/tree")
//    @RequiresPermissions("product:category:list")
    public R list(){
        List<CategoryEntity> categoryList = categoryServiceImpl.queryTreeList();
        return R.ok().put("data", categoryList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
//    @RequiresPermissions("product:category:info")
    public R catalogInfo(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryServiceImpl.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
        categoryServiceImpl.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @CacheEvict(value = "category",allEntries = true)       // 删除 category所有分组
//    @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
        if(StringUtils.isNotEmpty(category.getName())){
            CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
            categoryBrandRelationEntity.setCatelogId(category.getCatId());
            categoryBrandRelationEntity.setCatelogName(category.getName());
            categoryBrandRelationServiceImpl.updateName(categoryBrandRelationEntity);
        }
        categoryServiceImpl.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        categoryServiceImpl.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
